﻿using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using PageTypeBuilder.SortOrderChecker;

public class PageTypeBuilderSortOrderChecker : Task
{
    // http://www.developerfusion.com/article/84411/customising-your-build-process-with-msbuild/

    private ITaskItem _assemblyFolderPath;
    private ITaskItem _treatAsErrors;

    [Required]
    public ITaskItem AssemblyFolderPath
    {
        get { return _assemblyFolderPath; }
        set { _assemblyFolderPath = value; }
    }

    [Required]
    public ITaskItem TreatAsErrors
    {
        get { return _treatAsErrors; }
        set { _treatAsErrors = value; }
    }

    public override bool Execute()
    {
        string assemblyFolderPath = AssemblyFolderPath.ItemSpec.Trim();

        if (!Directory.Exists(assemblyFolderPath))
        {
            Log.LogError("Folder path '{0}' does not exist.", assemblyFolderPath);
            return false;
        }

        bool treatAsErrors = TreatAsErrors.ItemSpec.Trim() == "1";
        bool returnValue = new SortOrderChecker().CheckSortOrders(SortOrderChecker.RunType.BuildTask,
                                                                  assemblyFolderPath, treatAsErrors, this);

        return returnValue;
    }

}
